// JetBranins 注册机 + TLS服务授权认证
package main

import (
	"flag"
	"fmt"
	"keygen/subgroup"
	"math/rand"
	"time"
)

var gk = flag.String("getkey", "0", "getkey:  1 or 2  ,\"1\":生成签名证书(可用配合 -username 参数生成指定用户名证书)  \"2\":计算注册码  \"0\":启动WEB服务")
var licenseId = flag.String("keyid", "", "licenseId 自定义的注册码ID,可自行设置")
var licenseeName = flag.String("username", "johnroot", "licenseeName ")
var metaData = flag.String("md", "0120200728EPAJA801225", "metaData 版本相关")
var hash = flag.String("hash", "127988766/0", "Hash值")
var fallbackDate = flag.String("fallbackdate", "2099-05-20", "到期日期 ")
var rcode = flag.String("code", "all", "生成指定软件的注册码，all 是全部。")
var dp = flag.String("dump", "", "解析注册KEY")
var certName = flag.String("certname", "", "指定certName，会在启动web服务时创建证书")

// metaData: "0120200728EPAJA008006", hash: "12796877/0"
func main() {
	flag.Parse()

	switch {
	case *dp != "":
		fmt.Println(subgroup.DumpKey(*dp))
		var stop string
		fmt.Scanln(&stop)
	case *gk == "1":
		subgroup.CreateCrt(0, *licenseeName)
	case *gk == "2":
		if len(*licenseId) == 0 {
			sz := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
			tid := make([]byte, 10)
			rs := rand.NewSource(time.Now().UnixNano())
			for i := 0; i < len(tid); i++ {
				p := rand.New(rs).Intn(len(sz))
				tid[i] = []byte(sz)[p]
			}
			*licenseId = string(tid)
		}
		regCode := subgroup.CreateKey(licenseId, licenseeName, metaData, hash, fallbackDate, rcode)
		fmt.Println("注册码:", regCode)
		fmt.Scanln()
	default:
		if *certName != "" {
			subgroup.CreateCrt(1, *certName)
		}
		subgroup.StartWeb()
	}
}

//go生成一个http服务端
